<?php

namespace IDock\Insurance\TPBX\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\TPBX\TPBXEnums;
use IDock\Insurance\TPBX\Response\AllTPBXResponse;
use IDock\Printer\EPolicyDownloadPrinter;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllTPBXResponse
{

    /**
     * @var RenewalInquiryPrinter $printer
     */
    public $printer = RenewalInquiryPrinter::class;

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();
        $body = $this->getBody()['data'] ?? '';
        $renewalInquiryPrinter->argc['orderNo'] = $renewBase['orderNo'] ?? '';
        $renewalInquiryPrinter->setRenewalFlg(true);
        $biData = $body['bizApply'] ?? [];
        $ciData = $body['tpfApply'] ?? [];
        $this->printer->biPolicyNo = $biData['policyNo'] ?? '';
        $this->printer->biEndData = $biData['expireDate'] ? date('Y-m-d',strtotime($biData['expireDate'])) : '';
        $this->printer->biStartData = $biData['effectiveDate'] ? date('Y-m-d',strtotime($biData['effectiveDate'])) : '';
        $this->printer->ciPolicyNo = $ciData['policyNo'] ?? '';
        $this->printer->ciEndData = $ciData['expireDate'] ? date('Y-m-d',strtotime($ciData['expireDate'])) : '';
        $this->printer->ciStartData = $ciData['effectiveDate'] ? date('Y-m-d',strtotime($ciData['effectiveDate'])) : '';

        $vehicle = $body['vehicle'] ?? [];
        //车牌号
        $renewalInquiryPrinter->licenseNo = $vehicle['vehicleLicenceCode'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['vehicleFrameNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNo'] ?? '';
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['firstRegisterDate'] ?? '';
        //车辆信息
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['autoModelChnName'] ?? '';
        $vehiclePrinter->power = $vehicle['power'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['purchasePrice'] ?? '';
        $vehiclePrinter->vehicleExhaust = $vehicle['exhaustCapability'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['vehicleSeats'] ?? '';
        $vehiclePrinter->vehicleTonnage = round(floatval($vehicle['vehicleTonnages'] ?? '') * 1000,2);
        $vehiclePrinter->wholeWeight = $vehicle['weight'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['autoModelCode'] ?? '';
        $vehiclePrinter->energyTypeCode =  array_search($vehicle['fuelType'] ?? '',TPBXEnums::$energyType) ?? 'E01';

        //投保人
        $applicant = $body['applicant'] ?? [];
        $applicantPrinter = $this->relationInt($applicant);

        //被保人
        $insured = $body['insured'] ?? [];
        $insuredPrinter = $this->relationInt($insured);

        //车主
        $owner = $body['vehicleOwner'] ?? [];
        $ownerPrinter = $this->relationInt($owner);

        $coverageList = $body['coverageList'] ?? [];
        foreach ($coverageList as $coverage) {
            $coveragePrinter = new PremiumCoveragePrinter();
            $coveragePrinter->coverageCode = array_search($coverage['coverageCode'] ?? '', TPBXEnums::$coverageCode);
            if (!$coveragePrinter->coverageCode) {
                continue;
            }
            if(in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])){
                $coveragePrinter->coverageCode = 'C1' . substr($coveragePrinter->coverageCode, 2, 4);
            }
            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = (string)floatval($coverage['insuredAmount'] ?? '');
            $coveragePrinter->premium = (string)floatval($coverage['totalActualPremium'] ?? '');
            $coveragePrinter->serviceTimes = $coverage['serviceNum'] ?? '';
            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }
        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }

    private function relationInt($applicant)
    {
        $applicantPrinter = new PremiumRelationPrinter();
        if($applicant['personType'] == '1'){
            $applicant = $applicant['person'] ?? [];
            $applicantPrinter->name = $applicant['personnelName'] ?? '';
            $applicantPrinter->mobile = $applicant['mobileTelephone'] ?? '';
            $applicantPrinter->address = $applicant['address'] ?? '';
            $applicantPrinter->certificateNo = $applicant['certificateNo'] ?? '';
            $applicantPrinter->certificateTypeCode = array_keys(TPBXEnums::$certificateTypeCode, $applicant['certificateType'] ?? '')[0] ?? '';
            $applicantPrinter->email = $applicant['email'] ?? '';
            $applicantPrinter->certificateStartDate = $applicant['effectedDate'] ?? '';
            $applicantPrinter->certificateEndDate = $applicant['expiredDate'] ?? '';
            $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';
        }else{
            $applicant = $applicant['group'] ?? [];
            $applicantPrinter->name = $applicant['organName'] ?? '';
            $applicantPrinter->mobile = $applicant['linkManMobileTelephone'] ?? '';
            $applicantPrinter->address = $applicant['address'] ?? '';
            $applicantPrinter->certificateNo = $applicant['certificateNo'] ?? '';
            $applicantPrinter->certificateTypeCode = array_keys(TPBXEnums::$certificateTypeCode, $applicant['agentIdCardType'] ?? '')[0] ?? '';
            $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';
        }

        return $applicantPrinter;
    }
}
